package com.example.demo.service;

import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        List<User> users = userMapper.getUserByUsername(s);
        System.out.println(users.get(0).getId());
        System.out.println(users);

        if (null == users || users.size() ==0) {
            throw new UsernameNotFoundException("该用户不存在！");
        }else{
//            users.get(1).setRoles(roleMapper.getRolesByUserId(users.get(1).getId()));
//            System.out.println("+++++++++++++++++++++"+users.get(1).getAuthorities());
            users.get(0).setRoles(roleMapper.getRolesByUserId(users.get(0).getId()));
            System.out.println("***********************"+users.get(0).getAuthorities());
            return users.get(0);
        }

    }
}
